<template>
  <div class="login-page">
    <!-- 导航条 -->
    <van-nav-bar title="注册" />
    <!-- 中间表单 -->
    <van-form @submit="onSubmit">
      <van-field v-model.trim="username" name="username" label="用户名" placeholder="请输入用户名" :rules="[
        { required: true, message: '用户名不能为空' },
        { pattern: /^\w{5,}$/, message: '用户名至少包含5个字符' }
      ]" />
      <van-field v-model.trim="password" type="password" name="password" label="密码" placeholder="请输入密码" :rules="[
        { required: true, message: '密码不能为空' },
        { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' }
      ]" />
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <!-- 有账号, 去登 -->
    <router-link class="link" to="/login">有账号, 去登录</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      this.$toast.loading({
        message: '正在注册中...',
        forbidClick: true
      })

      await register(value)
      this.$toast.success('注册成功')
      this.$router.replace('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
